package cool.webstudy.admin.controller.dict;


import cn.hutool.core.lang.tree.Tree;
import cool.webstudy.admin.model.dto.dict.type.CreateDictTypeDTO;
import cool.webstudy.admin.model.dto.dict.type.UpdateDictTypeDTO;
import cool.webstudy.admin.model.vo.dict.type.QueryEnabledDictTypeRoughInfoVO;
import cool.webstudy.admin.service.DictDataService;
import cool.webstudy.admin.service.DictTypeService;
import cool.webstudy.admin.utils.aspect.request.RequestParameterCheck;
import cool.webstudy.admin.utils.common.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "字典类型管理")
@RestController
@RequestMapping("/dict-type")
@RequestParameterCheck
public class DictTypeController {
    @Autowired
    private DictTypeService dictTypeService;
    @Autowired
    private DictDataService dictDataService;

    @PostMapping("/create")
    @Operation(summary = "创建字典类型接口")
    public Result create(@RequestBody(required = true) CreateDictTypeDTO createDictTypeDTO){
        dictTypeService.createDictType(createDictTypeDTO);
        return Result.success();
    }

    @PostMapping("/delete")
    @Operation(summary = "删除字典类型")
    public Result delete(@RequestBody(required = true) String unCode){
        dictTypeService.deleteDictType(unCode);
        //TODO 可能会存在事务问题
        dictDataService.deleteDictDataByDictTypeUnCode(unCode);
        return Result.success();
    }

    @PostMapping("/update")
    @Operation(summary = "更新字典类型")
    public Result update(@RequestBody(required = true) UpdateDictTypeDTO dto){
        dictTypeService.updateDictType(dto);
        return Result.success();
    }
    @GetMapping("/query/tree")
    @Operation(summary = "获取树形字典类型数据接口")
    public Result queryTree(){
        List<Tree<String>> treeList = dictTypeService.queryDictTypeTree();
       return Result.success(treeList);
    }

    @GetMapping("/query/enabled/list")
    @Operation(summary = "获取启用的字典类型基本信息列表接口")
    public Result queryEnableList(){
        List<QueryEnabledDictTypeRoughInfoVO> vo = dictTypeService.queryEnabledDictTypeRoughInfoList();
        return Result.success(vo);
    }
}
